import { AxiosResponse } from 'axios'
import { transformTime1, transformCount } from '@/utils/util'

export const getKeyWordListFilter = (res: AxiosResponse) => {
  const { data: { result } } = res
  let albums
  let artists
  let songs
  let playLists
  if (!result) { return }
  if (result.albums && result.albums.length) {
    albums = result.albums.map(item => {
      const { id, name, artist: { name: artistName } } = item
      return {
        id,
        name,
        artistName
      }
    })
  }
  if (result.artists && result.artists.length) {
    artists = result.artists.map(item => {
      const { id, name } = item
      return {
        id,
        name
      }
    })
  }
  if (result.songs && result.songs.length) {
    songs = result.songs.map(item => {
      const { id, name, artists } = item
      const artistListStr = artists.map(item => {
        return item.name
      }).join(' ')
      return {
        id,
        name,
        artistListStr
      }
    })
  }
  if (result.playlists && result.playlists.length) {
    playLists = result.playlists.map(item => {
      const { id, name } = item
      return {
        id,
        name
      }
    })
  }
  return {
    albums,
    playLists,
    songs,
    artists,
    order: result.order
  }
}

export const convertSearchInfo = (res, type: string) => {
  const { result } = res.data
  switch (type) {
    case 'songs': {
      const songs = result.songs.map((song: any) => {
        return {
          id: song.id,
          name: song.name,
          picUrl: song.al.picUrl,
          alias: song.alia.length ? song.alia.join('/') : '',
          artists: song.ar.map((item) => { return { id: item.id, name: item.name } }),
          alName: song.al.name,
          alId: song.al.id,
          sq: song.st === 0,
          duration: transformTime1(song.dt),
          mv: song.mv,
          pop: song.pop
        }
      })
      return {
        songs,
        total: result.songCount
      }
    }
    case 'artists': {
      const artists = result.artists.map((artist: any) => {
        return {
          id: artist.id,
          name: artist.name,
          picUrl: artist.picUrl
        }
      })
      return {
        artists,
        total: result.artistCount
      }
    }
    case 'albums': {
      const albums = result.albums.map((album: any) => {
        return {
          id: album.id,
          name: album.name,
          picUrl: album.picUrl,
          artist: album.artist.name
        }
      })
      return {
        albums,
        total: result.albumCount
      }
    }
    case 'playlists': {
      const playlists = result.playlists.map((playlist: any) => {
        return {
          id: playlist.id,
          name: playlist.name,
          coverImgUrl: playlist.coverImgUrl,
          trackCount: playlist.trackCount,
          nickName: playlist.creator.nickname
        }
      })
      return {
        playlists,
        total: result.playlistCount
      }
    }
    case 'videos': {
      const videos = result.videos.map((video: any) => {
        return {
          id: video.vid,
          name: video.title,
          coverUrl: video.coverUrl,
          duration: transformTime1(video.durationms),
          playCount: transformCount(video.playTime),
          nickName: video.creator.map((creator) => { return creator.userName }).join('/')
        }
      })
      return {
        videos,
        total: result.videoCount
      }
    }
    default:
      break
  }
}
